package com.baizhi.service.impl;

import com.baizhi.entity.Admin;
import com.baizhi.mapper.AdminMapper;
import com.baizhi.service.IAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longyang
 * @since 2022-01-13
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin selectAdmin(Admin admin) {
        //条件构造器：用于构建sql条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        // where username = "xiaohei" and password = "123456"
        queryWrapper.eq("username",admin.getUsername()).eq("password",admin.getPassword()).eq("status",1);
        //查询
        Admin newAdmin = adminMapper.selectOne(queryWrapper);
        //返回user
        return newAdmin;
    }

    @Override
    public List<Admin> selectAdminAll() {
        List<Admin> admins = adminMapper.selectList(null);
        return admins;
    }

    @Override
    public Integer addAdmin(Admin admin) {
        return adminMapper.insert(admin);
    }

    @Override
    public Integer deleteAdmin(Integer id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public Admin selcetAdminById(Integer id) {
        //条件构造器：用于构建sql条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>();
        queryWrapper.eq("id",id);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateAdmin(Admin admin) {
        return adminMapper.updateById(admin);
    }
}
